app.controller("goodsManagementController",function ($scope,$controller,$location,goodsManagementService,goodsCategoryService,goodsTemplateService) {
    $controller("commonController",{$scope:$scope});
    //搜索
    $scope.searchEntity={}
    $scope.search=function(pageNum,pageSize){
        goodsManagementService.search(pageNum,pageSize,$scope.searchEntity).success(
            function(response){
                $scope.list=response.result;
                $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }
    //定义页面实体
    $scope.entity={dpGoods:{},dpGoodsDesc:{itemImages:[],specificationItems:[]}};
    //查询实体
    $scope.findOne=function(){
        var id=$location.search()['id'];
        //如果 id 为空，则返回，避免和添加商品时冲突
        if (id == null){
            return;
        }
        goodsManagementService.findOne(id).success(
            function(response){
                $scope.entity= response;
                //获取商品介绍，给到富文本编辑器
                editor.html($scope.entity.dpGoodsDesc.introduction);
                //转换图片路径为 json 对象，数据库中存的是 string 类型
                $scope.entity.dpGoodsDesc.itemImages=JSON.parse($scope.entity.dpGoodsDesc.itemImages);
                //转换扩展属性为 json 对象以供页面显示，因为数据库中存的是 string 类型
                $scope.entity.dpGoodsDesc.customAttributeItems=JSON.parse($scope.entity.dpGoodsDesc.customAttributeItems);
                //转换规格
                $scope.entity.dpGoodsDesc.specificationItems=JSON.parse($scope.entity.dpGoodsDesc.specificationItems);
                //转换 sku 表的 spec 规格选项
                for (var i=0;i<$scope.entity.dpItemList.length;i++){
                    $scope.entity.dpItemList[i].spec=JSON.parse($scope.entity.dpItemList[i].spec);
                }
            }
        );
    }
    //查询一级分类
    $scope.selectItemCat1List=function () {
        goodsCategoryService.findByParentId(0).success(
            function (response) {
                $scope.itemCat1List=response;
            }
        );
    }
    //查询二级分类
    $scope.$watch("entity.dpGoods.category1Id",function (newValue,oldValue) {
        goodsCategoryService.findByParentId(newValue).success(
            function (response) {
                $scope.itemCat2List=response;
            }
        );
    });
    //查询三级分类
    $scope.$watch("entity.dpGoods.category2Id",function (newValue,oldValue) {
        goodsCategoryService.findByParentId(newValue).success(
            function (response) {
                $scope.itemCat3List=response;
            }
        );
    });
    //模板变化后
    $scope.$watch("entity.dpGoods.typeTemplateId",function (newValue,oldValue) {
        //查询扩展属性和品牌列表
        goodsTemplateService.findOne(newValue).success(
            function (response) {
                //读取品牌列表
                $scope.brandList=JSON.parse(response.brandIds);
                // //判断如果 id 为空就执行，避免和显示商品信息时的冲突
                // if ($location.search()['id']==null){
                //     $scope.entity.dpGoodsDesc.customAttributeItems=JSON.parse(response.customAttributeItems);
                // }

            });
    });



    //显示状态信息
    $scope.status=["未申请","未审核","审核通过","驳回"];
    $scope.goodsCatList=[];
    $scope.findGoodsCatList=function (id) {
        goodsCategoryService.findAll().success(
            function (response) {
                for (var i=0;i<response.length;i++){
                    $scope.goodsCatList[response[i].id]=response[i].name;
                }
          }
        );
    }
    //更新商品状态
    $scope.updateAuditStatus=function (status) {
        goodsManagementService.updateAuditStatus($scope.selected,status).success(
            function (response) {
                if (200==response.code){
                    alert(response.message);
                    //刷新列表
                    $scope.reloadList();
                    //清空选择的 id
                    $scope.selected=[];
                }else{
                    alert(response.message);
                    $scope.selected=[];
                }
            }
        );
    }
    //逻辑删除
    //批量删除
    $scope.delete=function(){
        //获取选中的复选框
        goodsManagementService.delete( $scope.selected ).success(
            function(response){
                if(200==response.code){
                    $scope.reloadList();//刷新列表
                    $scope.selectIds=[];
                }
            }
        );
    }
});